package com.lt.flashcard.view;

import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Include;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.FlashcardSet;
import com.lt.flashcard.model.entity.User;
import com.lt.flashcard.model.service.AAService;
import com.lt.flashcard.model.service.FlashcardSetService;
import com.lt.flashcard.model.service.Repository;

public class AdminProductManagerCtrl extends SelectorComposer<Component> {

	private static final long serialVersionUID = 1L;
	
	@Wire
	private Textbox fcsname;
	@Wire
	private Textbox fcsauthor;
	@Wire
	private Include ctnFcsList;
	
	private int curpage = 0;
	private int pagesize = 20;
	private long total = 0;
	
	private User user;
	private List<FlashcardSet> flashcardsets = null;
	
	@Override
	public ComponentInfo doBeforeCompose(Page page, Component parent, ComponentInfo compInfo) {
		AAService srv  = Repository.getInstance().getAAService();
		if(!srv.isLogin() || !srv.hasRole("admin")) {
			Utility.redirect("/admin_login.zul", false);
			return null;
		}
		user = srv.getCurrentUser();
		FlashcardSetService fcssrv = Repository.getInstance().getFlashcardSetService();
		flashcardsets = fcssrv.getAllAdminFlashcardSets();
		curpage = 0;
		pagesize = flashcardsets.size();
		total = pagesize;
		return super.doBeforeCompose(page, parent, compInfo);
	}
	
	public void loadAdminList() {
		FlashcardSetService fcssrv = Repository.getInstance().getFlashcardSetService();
		flashcardsets = fcssrv.getAllAdminFlashcardSets();
		curpage = 0;
		pagesize = flashcardsets.size();
		total = pagesize;
		getCtnFcsList().invalidate();
	}
	
	public User getUser(){
		return user;
	}
	
	public void setUser(User user){
		this.user = user;
	}

	public List<FlashcardSet> getFlashcardsets() {
		return flashcardsets;
	}

	public void setFlashcardsets(List<FlashcardSet> flashcardsets) {
		this.flashcardsets = flashcardsets;
	}
	
	public Textbox getFcsauthor() {
		return fcsauthor;
	}

	public void setFcsauthor(Textbox fcsauthor) {
		this.fcsauthor = fcsauthor;
	}

	public Textbox getFcsname() {
		return fcsname;
	}

	public void setFcsname(Textbox fcsname) {
		this.fcsname = fcsname;
	}

	public Include getCtnFcsList() {
		return ctnFcsList;
	}

	public void setCtnFcsList(Include ctnFcsList) {
		this.ctnFcsList = ctnFcsList;
	}
	
	@Listen("onClick=#btnFilterFcs")
	public void doFilter() {
		curpage = 0;
		doPager();
		getCtnFcsList().invalidate();
	}
	
	public void doPager() {
		FlashcardSetService fcssrv = Repository.getInstance().getFlashcardSetService();
		flashcardsets = fcssrv.fiterFlashcardSets(fcsname.getValue(), fcsauthor.getValue(), curpage * pagesize, pagesize);
		total = fcssrv.countFilterFlashcardSets(fcsname.getValue(), fcsauthor.getValue());
	}
	
	@Listen("onClick=#btnCreateFcs")
	public void doCreateFcs() {
		((Window) getWindowComp("create_flashcardset")).doModal();
	}
	
	public Component getWindowComp(String task){
		Component comp = null;
		if(task.equals("create_flashcardset")) {
			comp = Executions.createComponents("/create_edit_flashcardset.zul", null, null);
		}
		return comp;
	}
	
	/**
	 * New flashcardset
	 */
	@Listen("onClick=#btnNewFCS")
	public void btnNewFCS() {
		Utility.redirect("admin_product_edit.zul", true);
	}

	/**
	 * @return the pagesize
	 */
	public int getPagesize() {
		return pagesize;
	}

	/**
	 * @param pagesize the pagesize to set
	 */
	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}

	/**
	 * @return the curpage
	 */
	public int getCurpage() {
		return curpage;
	}

	/**
	 * @param curpage the curpage to set
	 */
	public void setCurpage(int curpage) {
		this.curpage = curpage;
	}

	/**
	 * @return the total
	 */
	public long getTotal() {
		return total;
	}

	/**
	 * @param total the total to set
	 */
	public void setTotal(long total) {
		this.total = total;
	}
	
	public void notifyChangePage(int newpage) {
		curpage = newpage;
		doPager();
		getCtnFcsList().invalidate();
	}
}
